﻿using System.Collections;
using UnityEngine;

public class DMXManager : MonoBehaviour
{
    public static DMXManager instance;

    private ArtNetDMXController1 ArtNet1;
    private byte[] data1 = new byte[512];

    private ArtNetDMXController2 ArtNet2;
    private byte[] data2 = new byte[512];

    private int dmxValue;

    private void Awake()
    {
        instance = this;
    }

    void Start()
    {
        ArtNet1 = GetComponent<ArtNetDMXController1>();
        data1 = new byte[512];

        ArtNet2 = GetComponent<ArtNetDMXController2>();
        data2 = new byte[512];

        for (int i = 0; i < data1.Length; i++)
        {
            data1[i] = (byte)(i / 2);
        }

        for (int i = 0; i < data2.Length; i++)
        {
            data2[i] = (byte)(i / 2);
        }

        StartCoroutine(InitGameData());
    }

    private IEnumerator InitGameData()
    {
        yield return new WaitForSeconds(3.0f);
        SendDMXMsg(1, 255);
        SendDMXMsg(2, 255);
        SendDMXMsg(3, 255);
        SendDMXMsg(4, 255);
        SendDMXMsg(5, 255);
        Debug.Log("初始化灯带数据！");
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.O))
        {
            OpenLEDLight();
        }
        if (Input.GetKeyDown(KeyCode.C))
        {
            CloseLEDLight();
        }

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            SendDMXMsg(8, 1);
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            SendDMXMsg(8, 9);
        }
        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            SendDMXMsg(8, 17);
        }
        if (Input.GetKeyDown(KeyCode.Alpha4))
        {
            SendDMXMsg(8, 25);
        }
        if (Input.GetKeyDown(KeyCode.Alpha5))
        {
            SendDMXMsg(8, 33);
        }
        if (Input.GetKeyDown(KeyCode.Alpha6))
        {
            SendDMXMsg(8, 41);
        }
        if (Input.GetKeyDown(KeyCode.Alpha7))
        {
            SendDMXMsg(8, 49);
        }
        if (Input.GetKeyDown(KeyCode.Alpha8))
        {
            SendDMXMsg(8, 57);
        }
    }

    public void OpenLEDLight()
    {
        Debug.Log("打开LED灯光！");
        if (fadeIn5 != null)
        {
            StopCoroutine(fadeIn5);
        }

        fadeIn5 = StartCoroutine(StartlightEffectFadeIn(5));
    }

    public void CloseLEDLight()
    {
        Debug.Log("关闭LED灯光！");
        if (fadeOut5 != null)
        {
            StopCoroutine(fadeOut5);
        }

        StartCoroutine(StopAllLight(5));
    }

    private IEnumerator StopAllLight(int channel) {
        float lerpTime = 0f;
        int maxDMXValue = dmxValue;
        while (true) {
            yield return new WaitForSeconds(0f);
            lerpTime += Time.deltaTime;
            dmxValue = (int)( Mathf.SmoothStep(maxDMXValue, 0, lerpTime / 2f) );

            if (dmxValue == 0) {
                SendDMXMsg(channel, dmxValue);
                break;
            }
            //Debug.Log("LerpValueFadeOut :" + dmxValue);
            SendDMXMsg(channel, dmxValue);
        }
    }

    private Coroutine fadeIn1;
    private Coroutine fadeIn2;
    private Coroutine fadeIn3;
    private Coroutine fadeIn4;
    private Coroutine fadeIn5;

    private Coroutine fadeOut1;
    private Coroutine fadeOut2;
    private Coroutine fadeOut3;
    private Coroutine fadeOut4;
    private Coroutine fadeOut5;


    private IEnumerator StartlightEffectFadeIn(int channel)
    {
        dmxValue = 0;
        float lerpTime = 0f;
        while (true)
        {
            yield return new WaitForSeconds(0f);
            lerpTime += Time.deltaTime;
            dmxValue = (int)(Mathf.SmoothStep(0, 255, lerpTime / 2f));

            if (dmxValue == 255)
            {
                SendDMXMsg(channel, dmxValue);
                if (channel == 5)
                {
                    fadeOut5 = StartCoroutine(StartlightEffectFadeOut(5));
                }
                break;
            }
            //Debug.Log("LerpValueFadeIn :" + dmxValue);
            SendDMXMsg(channel, dmxValue);
        }


    }

    private IEnumerator StartlightEffectFadeOut(int channel)
    {
        dmxValue = 255;
        float lerpTime = 0f;

        while (true)
        {
            yield return new WaitForSeconds(0f);
            lerpTime += Time.deltaTime;
            dmxValue = (int)(Mathf.SmoothStep(255, 0, lerpTime / 2f));

            if (dmxValue == 0)
            {
                SendDMXMsg(channel, dmxValue);
                if (channel == 5)
                {
                    fadeIn5 = StartCoroutine(StartlightEffectFadeIn(5));
                }
                break;
            }
            //Debug.Log("LerpValueFadeOut :" + dmxValue);
            SendDMXMsg(channel, dmxValue);
        }

    }


    private void SendDMXMsg(int channel, int channelVolue)
    {
        byte volue = byte.Parse(channelVolue.ToString());
        data1[channel - 1] = volue;
        ArtNet1.Send(data1);
    }

}
